package com.icez.iosdatescroll

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.icez.base_util_library.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

open class IosDateScrollView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : RecyclerView(context, attrs) {

    // 是否第一次布局
    protected var isFirstLayout = true

    private var mOnScrollListener: OnScrollListener? = null

    private var mPointY = 0

    init {
        itemAnimator = null
    }

    open fun setIosDateScrollViewAdapter(recyclerWheelViewAdapter: IosDateScrollAdapter<*>) {
        layoutManager = LinearLayoutManager(context)
        // 一个item显示
        val snapHelper = LinearSnapHelper()
        snapHelper.attachToRecyclerView(this)
        adapter = recyclerWheelViewAdapter
        addScrollListener()
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        // 是否第一次布局
        if (isFirstLayout) {
            // 获取第一个控件
            val childView = getChildAt(1)
            if (null != childView) {
                // 获取第一个控件的中心点
                val centerY = childView.height / 2
                scrollBy(0, centerY)
                isFirstLayout = false
            }
        }
    }

    /**
     * @sample 滚动到指定的索引
     * @param newPosition 索引
     */
    public fun setScrollPosition(newPosition:Int){
        GlobalScope.launch( Dispatchers.Main) {
            if(adapter == null){
                delay(500)
            }
            if (adapter is IosDateScrollAdapter<*>) {
                val recyclerWheelViewAdapter = adapter as IosDateScrollAdapter<*>
                val childView = getChildAt(1)
                val position = newPosition - recyclerWheelViewAdapter.selectedItemPosition
                if (null != childView) {
                    // 获取第一个控件的中心点
                    val centerY = childView.height / 2
                    if(position<0 ){
                        smoothScrollBy(0, recyclerWheelViewAdapter.itemHeight*position)
                    }else if(position>0){
                        smoothScrollBy(0, recyclerWheelViewAdapter.itemHeight*position)
                    }

                }

            }
        }



    }

    /**
     * @sample 添加滚动监听
     */
    private fun addScrollListener() {
        if (mOnScrollListener == null) {
            mOnScrollListener = object : OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    mPointY += dy
                    if (adapter is IosDateScrollAdapter<*>) {
                        val recyclerWheelViewAdapter = adapter as IosDateScrollAdapter<*>
                        recyclerWheelViewAdapter.notifyScroll(mPointY)
                    }
                }
            }
            addOnScrollListener(mOnScrollListener!!)
        }
    }


}